﻿/**
 * Created by jiaxing on 2017/11/13.
 */
$(function () {
    $("#username").focus(function () {
        $(this).val("");
    })
    $("#password").focus(function () {
        $(this).val("");
    })
    function check(){
        var username = $("#username").val();
        var password = $("#password").val();
        if(username==""||password==""){
            alert("用户名或密码不能为空！")
            return 0;
        }
        else{
            return 1;
        }
    }
    $("#sub").click(
        function () {
            var username = $("#username").val();
            var password = $("#password").val();
            if(check()==1){
                //登录处理
                var obj = {
                    url:"/shixun/login?username="+ username +"&password="+ password,
                    dataType:"json",
                    type:"get",
                    success:function(data){
                        console.log(data);
                        if(data.re == "true"){
                            alert("登录成功！");
                            sessionStorage.setItem('username',username);
                            if(data.type=="student"){
                                alert("欢迎你！学生");
                                window.location.href = "/shixun/static/jiaoyu/xsgzpt.html"

                            }
                            else if(data.type=="teacher"){
                                alert("欢迎你！老师");
                                window.location.href = "../jiaoyu/teacher.html";

                            }
                            else if(data.type=="admin"){
                                alert("欢迎你！管理员");
                                window.location.href = "/shixun/static/xwglxt.html"
                            }
                            else{
                                console.log("错误！");
                            }
                        }
                        else{
                            alert("用户名或密码错误！");
                        }
                    },
                    error:function(e){
                        console.log(e);
                    }
                }
                $.ajax(obj);

            }
            else{
            }
            //location.href = "xsgzpt.html";
        }
    )
})